/*
* PROJECT: Happy New Year
* VERSION: 1.0
* AUTHOR: Mattias Thell
* URL: http://www.mattiasthell.com
*
* */

#include "Explosion.hpp"

#include "Settings.hpp"
#include "World.hpp"

namespace hny 
{
	Explosion::Explosion(World* world, hgeVector position, int combo, float velocity, float radius)
		:Actor(world, Kind_Explosion), mCombo(combo), mVelocity(velocity), mMaxRadius(radius)
	{
		mParticleSystem = ParticlePtr(new hgeParticleSystem(&Settings::GetInstance()->ExplosionParticleInfo));

		Actor::SetPosition(position);
		mParticleSystem->MoveTo(position.x, position.y);
		mParticleSystem->Fire();

		mRadiusSprite = SpritePtr(new hgeSprite(world->GetTexture("particles"), 0,0, 1.0f, 1.0f));
		mRadiusSprite->SetTextureRect(96, 64, 32, 32);
		mRadiusSprite->SetHotSpot(16, 16);

		mVelocity = 210.0f;

		mAge = 0.0f;
	}

	Explosion::~Explosion()
	{

	}

	void Explosion::Update(float dt) 
	{
		mAge += dt;
		mParticleSystem->Update(dt);
		float rad = GetRadius();

		float h = mRadiusSprite->GetHeight();
		float w = mRadiusSprite->GetWidth();

		if(rad < mMaxRadius)
			SetRadius(rad + dt*mVelocity);

		if(rad >= mMaxRadius)
			mSpriteAlpha = 0.0f;
		else
			mSpriteAlpha = 1.0f - (rad/mMaxRadius);

		// Check the age as well since 0 particles will be alive just at the beginning
		if(mParticleSystem->GetParticlesAlive() <= 0 && mAge > 0.1f)
			Kill();

		
	}

	void Explosion::Draw() 
	{
		mParticleSystem->Render();

		hgeVector pos(GetPosition());
		mRadiusSprite->SetColor(hgeColor(1.0f, 1.0f, 1.0f, mSpriteAlpha).GetHWColor());
		mRadiusSprite->RenderEx(pos.x, pos.y, 0.0f, GetRadius()/(mRadiusSprite->GetWidth()-10), GetRadius()/(mRadiusSprite->GetWidth()-10));
	}

	int Explosion::GetCombo() { return mCombo; }
}